package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.config.MyConfig;
import com.oceanread.cn.config.Result;
import com.oceanread.cn.entity.SideScan;
import com.oceanread.cn.service.ISideScanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import static com.oceanread.cn.controller.Drillhole1Controller.uploadFile;

@RestController
@RequestMapping("/SideScan")
@CrossOrigin()
@Api(value = "", tags = {"2.4-侧扫材质"})
public class SideScanController {

    @Autowired
    private ISideScanService iSideScanService;

    /*获取总表*/
    @GetMapping("/getall")
    public List<SideScan> getList() {
        return iSideScanService.getall();
    }

    @GetMapping("/getList")
    public JSONObject getList(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iSideScanService.getTablelist(page, size));
        object.put("total", iSideScanService.getnum());
        return object;
    }

    @GetMapping("/query1")
    public JSONObject query(
            @RequestParam(name = "page", required = true, defaultValue = "1") int page,
            @RequestParam(name = "size", required = true, defaultValue = "10") int size,
            @RequestParam(name = "key") String key,
            @RequestParam(name = "value") String value
    ) {
        JSONObject object = new JSONObject();
        object.put("data", iSideScanService.query(page, size, key, value));
        object.put("total", iSideScanService.getnumquery(key, value));
        return object;
    }
    @ApiOperation(value = "前端可视化 获取侧扫数据 URL")
    @GetMapping("/getinfo")
    public List<SideScan> equal( @RequestParam(name = "PrjCode") String PrjCode
    ) {
        return iSideScanService.equal("PrjCode", PrjCode);
    }

    @PostMapping("/update")
    public Integer update(@RequestBody SideScan sideScan) {
        return iSideScanService.update(sideScan);
    }

    @PostMapping("/insert")
    public Integer insert(@RequestBody SideScan sideScan) {
        return iSideScanService.insert(sideScan.getPrjCode(), sideScan.getCode(), sideScan.getName(), sideScan.getArea(), sideScan.getTime(),
                sideScan.getCenterCoordinateX(), sideScan.getCenterCoordinateY(), sideScan.getCenterCoordinateZ(), sideScan.getUrl());
    }

    @GetMapping("/delete")
    public Integer delete(@RequestParam(name = "SYSID") int sysid) {
        return iSideScanService.delete(sysid);
    }

    @RequestMapping(value = "/uploadPhoto",method = RequestMethod.POST)
    @ResponseBody
    public Result uploadPhoto(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        //设置文件上传路径
        String filePath = MyConfig.getFilepath() + "image/SideScan/";
        System.out.println(filePath + fileName);
        try {
            uploadFile(file.getBytes(), filePath, fileName);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    @RequestMapping(value = "/getPhoto/{imgUrl}", produces = MediaType.IMAGE_JPEG_VALUE,method = RequestMethod.GET)
    @ResponseBody
    public byte[] getPhoto(@PathVariable("imgUrl") String imgUrl) {
        try {
            System.out.println(imgUrl);
            File file = new File(MyConfig.getFilepath() + "image/SideScan/" + imgUrl);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
            return bytes;
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/uploadGeojson",method = RequestMethod.POST)
    @ResponseBody
    public Result uploadGeojson(@RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        //设置文件上传路径
        String filePath = MyConfig.getFilepath() + "json/SideScan/";
        System.out.println(filePath + fileName);
        try {
            uploadFile(file.getBytes(), filePath, fileName);
            return Result.success();
        } catch (Exception e) {
            return Result.error();
        }
    }

    @ApiOperation(value = "SideScan统一插入")
    @RequestMapping(value = "/SideScanInsert", method = RequestMethod.POST)
    public void inserttest(HttpServletRequest request, HttpServletResponse response) {
        try {
            BufferedReader reader = request.getReader();
            if (reader != null) {
                String redaline = reader.readLine();
                JSONObject readobject = JSONObject.parseObject(redaline);
                JSONObject data = readobject.getJSONObject("data");
                System.out.println(data);
                String PrjCode = data.getString("PrjCode");
                String Name = data.getString("Name");

                String url = data.getString("url");
                SideScan sideScan = new SideScan();
                sideScan.setPrjCode(PrjCode);
                sideScan.setName(Name);
                sideScan.setUrl(url);
                iSideScanService.insert(sideScan.getPrjCode(), sideScan.getCode(), sideScan.getName(), sideScan.getArea(), sideScan.getTime(),
                        sideScan.getCenterCoordinateX(), sideScan.getCenterCoordinateY(), sideScan.getCenterCoordinateZ(), sideScan.getUrl());

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

